package zj.chunan.menu.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import zj.chunan.db.DBconnection;
import zj.chunan.menu.model.TreeNode;

/**
 * @author user
 *
 */
public class TreeNodeDao {
	// ˽�б�
    private static TreeNodeDao dao;  
    // DBConnection����
    DBconnection dbc = new DBconnection();
    // 构造方法
	public TreeNodeDao() {
	}
	// 初始化Instanece方法
	public static TreeNodeDao getInstance(){  
		if(null == dao){  
			dao = new TreeNodeDao();  
		}  
		return dao;  
	}  
	
	/** 
     * 从数据库中返回父节点id号 ȡ�������
     * @return 父节点ID
     */  
    public StringBuilder get(){  
        Connection conection = null;  
        Statement stmt = null;  
        //   SQL语句生成 
        String sql="SELECT parentid FROM treenode WHERE parentid>0 Group By parentid Order By parentid"; 
        StringBuilder s= new StringBuilder(); 
        ResultSet rs = null; 
        	try{ 
        		conection = dbc.getConnection();   
                stmt = conection.createStatement();  
                rs = stmt.executeQuery(sql.toString());  
        		while(rs.next()) { 
	        	   s.append('|'); 
	        	   s.append(rs.getInt("parentid")); 
        		} 
           }catch(SQLException e){ 
        	   e.printStackTrace(); 
           } 
           return s; 
    } 
    
    /** 
     * 从数据库返回JSONArray字符串对象类 
     * @paramȡ父节点ID
     * @return 子节点的字符串
     */  
    public StringBuilder getPid(Serializable pid){
        Connection conection = null;  
    	Statement stmt = null; 
    	ResultSet rs = null; 
        // 取得子节点
        StringBuilder s= new StringBuilder(); 
//    	StringBuilder s=this.getResulsetRow(conection); 
    	ArrayList al=new ArrayList(); 
    	StringBuilder node=new StringBuilder("["); 
    	String sql="SELECT * FROM treenode WHERE parentid="+pid+" ORDER BY tid"; 
    	try{ 
    			conection = dbc.getConnection();       		
//    			pst = dbc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);  
                stmt = conection.createStatement();  
    			rs = stmt.executeQuery(sql); 
    			rs.last(); 
    			int i=rs.getRow(); 
    			System.out.println(i); 
    			rs.first(); 
    			while(rs.next()) { 
    					String p=String.valueOf(rs.getInt("tid")); 
    					node.append("{id:'"+p+"',text:'"+rs.getString("text").trim()+"',href:'"+rs.getString("href").trim()+"',"); 
//    					没有用set和get方法，好像不太好用，这里用了我自己的写法，    	
//			    	    TreeNode td=new TreeNode(); 
//			    	    td.setTid(rs.getInt("tid")); 
//			    	    td.setText(rs.getString("text").trim()); 
//			    	    td.setHref(rs.getString("href").trim()); 
    					if(s.indexOf("|"+p+"|")!=-1){ 
    							node.append("cls:'folder',leaf:"+false+"}"); 
//								    	    td.setCls("folder"); 
//								    	    td.setLeaf(false); 
    					}else{ 
    							node.append("cls:'file',leaf:"+true+"}"); 
								//    	    td.setCls("file"); 
								//    	    td.setLeaf(true); 
    					} 
    					int row=rs.getRow(); 
    					System.out.println(row); 
    					if(row<i){ 
    						node.append(","); 
    					} 
    					//    	     al.add(td); 
    			} 
    	   
    	  } catch(SQLException e){ 
    	   e.printStackTrace(); 
    	  } 
    	  node.append("]"); 
    	  return node;
    }
    
    /**
	 * @param args
	 */
	public static void main(String args[]){
		TreeNodeDao dao1 = new TreeNodeDao();
		StringBuilder treeStringBuilder = dao1.get();

		StringBuilder treeStringBuilder1 = dao1.getPid("-100");
		System.out.println(treeStringBuilder);
		System.out.println(treeStringBuilder1);
	}
}
